package com.cskaoyan.order.service.impl;


import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author LY
 * @since 2022/06/18 14:23
 */
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    /**
     * 获取用户订单
     * @param request
     * @return
     */
    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        OrderListResponse response = new OrderListResponse();

        try {
            // 参数校验
            request.requestCheck();

            // 分页插件
            PageHelper.startPage(request.getPage(), request.getSize());

            // 获取用户订单信息
            Example example = new Example(Order.class);
            example.createCriteria().andEqualTo("userId", request.getUserId());
            example.setOrderByClause("create_time " + request.getSort());
            List<Order> orderList = orderMapper.selectByExample(example);
            PageInfo<Order> pageInfo = new PageInfo<>(orderList);
            long total = pageInfo.getTotal();
            List<OrderDetailInfo> detailInfoList = orderConverter.orders2details(orderList);

            // 根据订单id获取订单商品信息和物流信息
            for (OrderDetailInfo orderDetailInfo : detailInfoList) {
                String orderId = orderDetailInfo.getOrderId();
                List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
                List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
                OrderShipping orderShipping = orderShippingMapper.queryByOrderId(orderId);
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
                orderDetailInfo.setOrderItemDto(orderItemDtos);
                orderDetailInfo.setOrderShippingDto(orderShippingDto);
            }

            response.setDetailInfoList(detailInfoList);
            response.setTotal(total);
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }

        return response;
    }

    /**
     * 获取订单详情
     * @param request
     * @return
     */
    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest request) {
        OrderDetailResponse response = null;
        try {
            // 参数验证
            request.requestCheck();

            String orderId = request.getOrderId();
            Order o = new Order();
            o.setOrderId(orderId);
            Order order = orderMapper.selectByPrimaryKey(o);
            response = orderConverter.order2res(order);


            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
            OrderShipping orderShipping = orderShippingMapper.queryByOrderId(orderId);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
            response.setOrderItemDto(orderItemDtos);
            response.setOrderShippingDto(orderShippingDto);

            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
